import json
import pandas as pd
from data_objects import Mashup, Api

'''
解析json文件
mashup_list 存储mashup信息
'''
with open('.\\active_mashups_data.txt','r',encoding='utf8')as fp:
    json_data = json.load(fp)
    mashup_list = []
    for a in json_data:
        mu = Mashup()
        mu.title = a['title']
        mu.tags = a['tags']
        mu.description = a['description']
        related_apis = a['related_apis']
        for b in related_apis:
            if b == None: break
            apis = Api()
            apis.title = b['title']
            tags = b['tags']
            for c in tags:
                apis.add_tags(tags)
            apis.description = b['description']
            apis.url = b['url']
            versions = b['versions']
            for cc in versions:
                apis.add_versions(cc['style'])
            mu.add_related_api(apis)
        categories = a['categories']
        for d in categories:
            mu.add_category(d)
        mu.url = a['url']
        mu.mashup_type = a['mashup_type']
        mu.date = a['date']
        mashup_list.append(mu)
'''
{
    "title": "Envato API MASTER RECORD",
    "tags": [
      "eCommerce",
      "Marketplace"
    ],
    "description": "The Envato API allows developers to access and search Envato's market information, retrieve user details, get Envato market stats, and check on the Envato forums from their own applications. Envato is a website that operates digital goods marketplaces and tutorial networks. Interactive documentation is provided for all of the API functions currently supported.",
    "url": "/api/envato\n",
    "versions": [
      {
        "version_title": "Envato REST API",
        "style": "REST",
        "version": "Recommended (active, supported)",
        "status": "N/A",
        "submit_date": "03.02.2011",
        "version_link": "/api/envato-rest-api"
      }
    ]
  },
'''
with open('.\\active_apis_data.txt','r',encoding='utf8')as fp1:
    json_data = json.load(fp1)
    api_list = []
    for a in json_data:
        if a == None: continue
        api_list.append(a['title'])

'''
数据分析
1. 统计Mashup中的包含Web API个数、Web API被使用的次数和Web API提供商发布Web API的个数。
'''

web_api_cnt = {}
for mashup in mashup_list:
    for api in mashup.related_apis:
        a = api.title
        if a in web_api_cnt.keys():
            web_api_cnt[a] += 1
        else:
            web_api_cnt[a] = 1
print("Mashup中Web Api个数为: " + str(len(web_api_cnt.keys())))
print("Mashup中Web Api被使用次数如下: ")
for item in web_api_cnt.keys():
    print(item + " " + str(web_api_cnt[item]))
print("Web Api提供商发布的Api个数: " + str(len(api_list)))


'''
2. 从需求关键词视角，分析在不同标注Tag或者Category类别中， 编程开发人员的组合需求（Mashup）与该需求所调用的服务（Web API）的关联情况。
'''
'''
for mashup in mashup_list:
    print(mashup.categories)
    for api in mashup.related_apis:
        print(api.title, end=',')
    print()
'''

categories_apis = {}
for mashup in mashup_list:
    cate = mashup.categories
    apilis = []
    for api in mashup.related_apis:
        apilis = api.title
    for c in cate:
        if c in categories_apis.keys():
            for api in mashup.related_apis:
                categories_apis[c].append(api.title)
        else:
            categories_apis[c] = []
            for api in mashup.related_apis:
                categories_apis[c].append(api.title)
for cate in categories_apis.keys():
    print(cate)
    apis_cnt = {}
    for api in categories_apis[cate]:
        if api in apis_cnt.keys():
            apis_cnt[api] += 1
        else:
            apis_cnt[api] = 1
    apis_cnt_s = sorted(apis_cnt.items(), key = lambda kv:(kv[1], kv[0]), reverse=True)
    for item in apis_cnt_s:
        print(item)
    print()

'''
3. 从非功能视角（例如API之间的兼容性，Web API不同的服务接口协议REST、RPC），对于相同标注下的Web API，非功能不同对于参与服务组合的情况统计。
'''

categories_apistyle = {}
for mashup in mashup_list:
    cate = mashup.categories
    apilis = []
    for api in mashup.related_apis:
        apilis = api.version
    for c in cate:
        if c in categories_apistyle.keys():
            categories_apistyle[c].append(apilis)
        else:
            categories_apistyle[c] = []
            categories_apistyle[c].append(apilis)
for key in categories_apistyle.keys():
    print(key)
    apistyle = categories_apistyle[key]
    result = pd.value_counts(apistyle)
    print(result)


